/**
 * @file    woshMacros.h
 * @brief   common MACROs
 *
 *
 ****************************************************************************
 * @version 0.01 $Id: woshMacros.h 2866 2010-08-07 15:01:48Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ******************************************************************************/

#ifndef __WOSH_MACROS_H__
 #define __WOSH_MACROS_H__

 #include <woshConfig.h>

 
 #define WSUCCEEDED(x)				( x == WRET_OK )
 #define WFAILED(x)					( x != WRET_OK )

 #define _GET_Max(a,b)				( (a>b)  ? a : b )
 #define _GET_Min(a,b)				( (a<b)  ? a : b )
 #define _GET_Abs(a)				( (a>=0) ? a :-a )

///////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////// ENUMS

 #define _ENUM_CmpStr(input_str, typeAsString, return_type) {			\
	if ( strcmp(typeAsString, input_str) == 0 ) return return_type; };


//
// out-of-bound safe, enum values and array index must be simmetric
// array values may be NULL
// sample:
/*
 const char* LogLevelStr[] = { "UNKNOWN", "NONE", NULL, .. };
EnumChkArray(LogContext::getLogLevelAsString, LogLevel, LOG_UNKNOWN, LOG_ALL, 0, LogLevelStr)

RESULTS:
const char* LogContext::getLogLevelAsString( LogLevel level ) {
	if ( level < LOG_UNKNOWN || level > LOG_ALL )
		return LogLevelStr[0];
	return LogLevelStr[level];
 }
*/
#undef Enum2StrFromArray
#define Enum2StrFromArray(FUNCTION, ENUMTYPE, MIN_ENUM, MAX_ENUM, DEFAULT, STR_ARRAY)	\
	const char* FUNCTION ( ENUMTYPE value ) {											\
		if ( value < MIN_ENUM || value > MAX_ENUM )										\
			return STR_ARRAY[0];														\
		return STR_ARRAY[value];														\
	 }

// out-of-bound & NULL safe, for users, not used internally
#undef Str2EnumByArray
#define Str2EnumByArray(FUNCTION, ENUMTYPE, MIN_ENUM, MAX_ENUM, DEFAULT, STR_ARRAY)		\
	ENUMTYPE FUNCTION ( const char* enum_string ) {										\
		if ( enum_string == NULL ) return DEFAULT;										\
		int i = MIN_ENUM;																\
		while ( i<static_cast<int>(MAX_ENUM) ) {										\
		if ( STR_ARRAY[i] == NULL ) { ++i; continue; }									\
			if ( strcmp(STR_ARRAY[i], enum_string) != 0 ) { ++i; continue; }			\
			return static_cast<ENUMTYPE>(i);											\
			++i;																		\
		 }																				\
		return DEFAULT;																	\
	 }
 

//////////////////////////////////////////////////////////////////////////////////////////// ENUMS
///////////////////////////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////// DEBUG

// #define NOT_IMPLEMENTED( x )		{ printf("==> Not implemented: file" __FILE__ " @line " __LINE__"\n"); };

 #ifndef ASSERT
 # ifndef DEBUG_ASSERT
 #  define ASSERT(x)
 # else
 #  define NDEBUG 
 #  include <cassert>
 #  define ASSERT(x)	assert(x);
 # endif
 #endif

//////////////////////////////////////////////////////////////////////////////////////////// DEBUG
///////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////// PROFILING
 // defined in SystemProfiler.h, class wosh::SystemProfiler
 // avoid many eventually confusing errors when profiling is disable
 #ifndef _ENABLE_PROFILING
 # define PROFILING_INIT( OBJECT, PARENT ) ;
 # define PROFILING_ENTER( OBJECT, ARGUMENT ) ;
 # define PROFILING_EXIT( OBJECT, ARGUMENT ) ;
 # define WOSH_DEPRECATE( OLD_FUNC_NAME, NEW_FUNC_NAME ) ;
 #endif
//////////////////////////////////////////////////////////////////////////////////////// PROFILING
///////////////////////////////////////////////////////////////////////////////////////////////////


#endif // __WOSH_MACROS_H__
